/**
 * 获取table列表数据（支持分页和随机mock）
 */

const nameList = ['张三', '李四', '王武', '赵六', '钱七', '孙八', '周九', '吴十'];
const addressList = ['北京市', '天津市', '河北省', '山西省', '内蒙古自治区', '辽宁省', '吉林省', '黑龙江省', '河南省'];
const ageList = [18, 22, 25, 30, 35, 40, 45, 50];

// 工具函数：从数组中随机取一个元素
const getRandomItem = (arr: any[]) => arr[Math.floor(Math.random() * arr.length)]

// 生成随机入职时间（格式为 YYYY-MM-DD）
const getRandomEntryTime = () => {
    const start = new Date('2019-06-01').getTime()
    const end = new Date('2025-01-01').getTime()
    const randomTimestamp = Math.floor(Math.random() * (end - start)) + start
    // const date = new Date(randomTimestamp)
    // const yyyy = date.getFullYear()
    // const mm = String(date.getMonth() + 1).padStart(2, '0')
    // const dd = String(date.getDate()).padStart(2, '0')
    // return `${yyyy}-${mm}-${dd}`
    return randomTimestamp
}

// 获取 100 ~ 2000 之间的随机整数
const getRandomFee = () => {
    const min = 100
    const max = 2000
    return Math.floor(Math.random() * (max - min + 1)) + min
}

export const getTableList = async (params: { currentPage: number; pageSize: number }) => {
    const {currentPage = 1, pageSize = 10} = params

    return new Promise((resolve) => {
        setTimeout(() => {
            const list = Array.from({length: pageSize}, (_, index) => {
                const id = (currentPage - 1) * pageSize + index + 1
                return {
                    id,
                    name: getRandomItem(nameList),
                    age: getRandomItem(ageList),
                    address: getRandomItem(addressList),
                    entryTime: getRandomEntryTime(),
                    fee: getRandomFee()
                }
            })

            resolve({
                code: 200,
                data: {
                    list,
                    total: 90 // 可调整模拟总数
                }
            })
        }, 500) // 模拟请求延迟
    })
}
